package cn.javabb.service;

import cn.javabb.common.OrderFileWrapper;
import cn.javabb.entity.CacheFile;
import cn.javabb.mapper.CacheFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CacheFileService {

    @Autowired
    CacheFileMapper cacheFileMapper;

    public String readMenu(String folerName,Integer level){
        CacheFile floder = new CacheFile();
        floder.setServerFilename(folerName);
        floder.setIsdir(1);
        List<CacheFile> floderList = cacheFileMapper.select(floder);
        String menuTxt = " 资源目录：\n ";
        if(null!=floderList&& floderList.size()>0){
            CacheFile floderNew = floderList.get(0);
            List<CacheFile> menuList = cacheFileMapper.listByParentPath(floderNew.getParentPath()+floderNew.getServerFilename()+"/");
            if(null == menuList){
                System.out.println("资源目录为空");
                return "";
            }
            menuTxt += listByFolderName(floderNew,level,0)+" \n \n";
        }else{
            System.out.println("资源找不到");
            return "";
        }
        return menuTxt;
    }
    
    public String listByFolderName(CacheFile floderNew,Integer level,Integer temp){
        String menuTxt = "";
            List<CacheFile> menuList = cacheFileMapper.listByParentPath(floderNew.getParentPath()+floderNew.getServerFilename()+"/");
            if(null == menuList){
                System.out.println("资源目录为空");
                return "";
            }
            OrderFileWrapper.naturalSort(menuList);
            for(CacheFile cf:menuList){
                if(cf.getServerFilename().indexOf("下载必看")>-1&&cf.getIsdir()==0){
                    continue;
                }
                System.out.println(formatter(cf.getServerFilename(),temp));
                if(cf.getIsdir()==0){
                    menuTxt +=formatter(cf.getServerFilename(),temp)+" \n ";
                }else{
                    menuTxt +=formatter(cf.getServerFilename(),temp)+" \n ";
                    if(level>1){
                        temp = temp+1;
                        menuTxt += listByFolderName(cf,level-1,temp);
                    }
                    temp=0;
                }


            }
        return menuTxt;
    }
    public String formatter(String name,int temp){
        String str = name.replace("[www.javadaba.com Java大巴]","")
                .replace("【www.javadaba.com Java大巴】", "")
                .replace("www.javadaba.com", "")
                .replace("javadaba.com", "")
                .replace("bbs.javabb.cn", "");
        String table = "┠━━";
        for(int i=0;i<temp;i++){
            table += "━━";
        }
        str = table+str;
        return str;
    }
    public static void main(String[] args) {
    }
}
